package com.example.administrator.demo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.example.administrator.demo.recycle.RecyclerViewAdapter;
import com.example.administrator.demo.recycle.RecyclerViewRefreshAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * projectName rxjavademo
 * date 2017/7/21
 * description com.example.administrator.demo.recycle
 */

public class RecyclerViewRefreshActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<String> mDatas;
    private RecyclerViewRefreshAdapter<String> mAdapter;

    private XRefreshView refreshView;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_refreshn);
        initData();
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        refreshView = (XRefreshView) findViewById(R.id.xrefreshview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        setXRefreshView();
        initRecyclerView();
    }

    protected void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++)
        {
            mDatas.add("" + (char) i);
        }
    }

    private void initRecyclerView(){
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new RecyclerViewRefreshAdapter<String>(this, R.layout.item_home, mDatas){
            @Override
            public void convert(CommonViewHolder holder, String bean, int position) {
                TextView view = holder.getView(R.id.id_num);
                view.setText(bean);
            }
        };
        layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void setXRefreshView(){
        //设置刷新完成以后，headerview固定的时间
        refreshView.setPinnedTime(600);
        refreshView.setMoveForHorizontal(true);
        refreshView.setPullLoadEnable(true);
        refreshView.setAutoLoadMore(false);
        refreshView.enableReleaseToLoadMore(true);
        refreshView.enableRecyclerViewPullUp(true);
        refreshView.enablePullUpWhenLoadCompleted(true);
        //设置静默加载时提前加载的item个数
//        xRefreshView1.setPreLoadCount(4);
        // 设置静默加载模式
//        xRefreshView1.setSilenceLoadMore();
        // 静默加载模式不能设置footerview
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
            }

            @Override
            public void onLoadMore(boolean isSilence) {
            }
        });
    }
}
